<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="name" placeholder="关键词" />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()"
        >查询</el-button
      >

      <el-button type="default" @click="resetData()">清空</el-button>
      <el-button
        type="success"
        @click="addForm"
        v-show="ROLE === 'admin' || ROLE === 'band'"
        >添加乐队</el-button
      >
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
    >
      <el-table-column prop="followed" label="关注" width="100" align="center">
        <template slot-scope="scope">
          <el-button
            :type="scope.row.followed ? 'success' : 'warning'"
            size="mini"
            @click="followBand(scope.row.id)"
          >
            {{ scope.row.followed ? "已关注" : "+关注" }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="name" label="乐队名" width="80" />

      <el-table-column prop="description" label="乐队描述" />
      <el-table-column prop="memberCount" label="成员数量" width="80" />

      <el-table-column prop="createTime" label="成立时间" width="160">
        <template slot-scope="scope">
          {{ new Date(scope.row.createTime).toLocaleDateString() }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="selectMembers(scope.row.id)"
            >成员</el-button
          >

          <el-button
            type="text"
            @click="edit(scope.row.id)"
            v-show="ROLE === 'admin' || ROLE === 'band'"
            >编辑</el-button
          >

          <el-button
            type="text"
            @click="removeDataById(scope.row.id)"
            v-show="ROLE === 'admin' || ROLE === 'band'"
            >删除</el-button
          >
        </template></el-table-column
      ></el-table
    >

    <!-- 修改成员窗口 -->
    <el-dialog title="编辑信息" :visible.sync="editMemberVisible">
      <el-form label-width="80px" :model="member">
        <el-form-item label="名字">
          <el-input v-model="member.name"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <template>
            <el-radio v-model="member.gender" :label="0">女</el-radio>
            <el-radio v-model="member.gender" :label="1">男</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number
            v-model="member.age"
            @change="handleChange"
            :min="1"
            :max="100"
            label="年龄"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="乐队分工">
          <el-input v-model="member.role"></el-input>
        </el-form-item>
        <el-form-item label="加入时间">
          <el-date-picker
            v-model="member.joinTime"
            type="date"
            format="yyyy-MM-dd"
            disabled
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editMemberVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateMember">保 存</el-button>
      </div>
    </el-dialog>

    <!-- 成员列表窗口 -->
    <el-dialog
      title="乐队成员"
      :visible.sync="membersVisible"
      width="60%"
      height="80%"
    >
      <el-button
        type="success"
        @click="addMember"
        plain
        v-show="ROLE === 'admin' || ROLE === 'band'"
        >添加成员</el-button
      >
      <el-table :data="members">
        <!-- 表格的其他表单项代码 -->
        <el-table-column property="name" label="姓名"> </el-table-column>
        <el-table-column property="gender" label="性别">
          <template slot-scope="scope">
            <span v-if="scope.row.gender === 0">女</span>
            <span v-else>男</span>
          </template>
        </el-table-column>
        <el-table-column property="age" label="年龄"> </el-table-column>
        <el-table-column property="role" label="分工"> </el-table-column>
        <el-table-column property="joinTime" label="加入时间">
          <template slot-scope="scope">
            {{ new Date(scope.row.joinTime).toLocaleDateString() }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="160"
          v-if="ROLE === 'admin' || ROLE === 'band'"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="editMember(scope.row.id)"
              icon="el-icon-edit"
              circle
              v-show="ROLE === 'admin' || ROLE === 'band'"
            ></el-button>
            <el-button
              type="danger"
              @click="deleteMember(scope.row.id)"
              icon="el-icon-delete"
              circle
              v-show="ROLE === 'admin' || ROLE === 'band'"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="membersVisible = false">取 消</el-button>
        <el-button type="primary" @click="membersVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑窗口 -->
    <el-dialog title="编辑乐队" :visible.sync="editVisible">
      <el-form label-width="80px" :model="band">
        <el-form-item label="名称">
          <el-input v-model="band.name"></el-input>
        </el-form-item>
        <el-form-item label="乐队描述">
          <el-input v-model="band.description"></el-input>
        </el-form-item>
        <el-form-item label="队长">
          <el-select v-model="band.leaderId">
            <el-option
              v-for="item in members"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateBand">保 存</el-button>
      </div>
    </el-dialog>
    <!-- 添加窗口 -->
    <el-dialog title="添加乐队" :visible.sync="dialogFormVisible">
      <el-form label-width="80px" :model="band">
        <el-form-item label="名称">
          <el-input v-model="band.name"></el-input>
        </el-form-item>
        <el-form-item label="乐队描述">
          <el-input v-model="band.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBand">保 存</el-button>
      </div>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
  <script>
//引入js文件
import {
  getBandListPage,
  deleteBand,
  addBand,
  selectBandById,
  selectMembers,
  updateBand,
  deleteMember,
  selectMember,
  updateMember,
  addMember,
} from "@/api/band";
import { follow } from "@/api/follow";
import { getToken } from "@/utils/auth";
import { getInfo } from "@/api/user";
export default {
  // data:{}
  data() {
    return {
      //用户角色
      ROLE: "",
      member: {
        age: 0,
        gender: 0,
        bandId: -1,
      },
      editMemberVisible: false,
      membersVisible: false,
      editVisible: false,
      dialogFormVisible: false,
      members: [], //乐队成员
      band: {
        name: "",
        description: "",
      },
      listLoading: true, //是否显示“数据加载中这几个字”

      bandId: 0,
      list: [], //封装返回的数据
      total: 0, //数据条数
      currentPage: 1, //当前页码
      pageSize: 5, //每页显示条数
      name: "",
    };
  },
  created() {
    this.setUserRole();
    this.getList();
  },
  methods: {
    //设置用户角色
    setUserRole() {
      let userId = getToken();
      getInfo(userId).then((res) => {
        this.ROLE = res.data.roles[0];
      });
    },
    //关注、取关
    followBand(id) {
      let userId = getToken();
      let type = 0; //0代表乐队
      let entityId = id;
      follow(userId, type, entityId).then((res) => {
        this.$message.success("成功");
        this.getList();
      });
    },
    //添加成员
    addMember() {
      //先将原对话框中的数据清空
      this.member = {};
      // console.log(this.bandId);
      //复用编辑对话框，不需要回显
      this.editMemberVisible = true;
    },
    //修改成员信息
    updateMember() {
      //判断是添加方法还是修改方法，根据member中的id是否存在
      // console.log(this.member.id);
      if (this.member.id === undefined) {
        //设置bandId
        this.member.bandId = this.bandId;
        //调用添加方法
        console.log("add");
        addMember(this.member).then((res) => {
          this.$message.success("添加成功");
          this.getList();
        });
      } else {
        console.log("edit");
        updateMember(this.member).then((res) => {
          this.$message.success("修改成功");
          this.getList();
        });
      }
      // 关闭成员信息框
      this.editMemberVisible = false;
      this.membersVisible = false;
    },
    //编辑成员
    editMember(id) {
      this.editMemberVisible = true;
      //回显
      selectMember(id).then((res) => {
        this.member = res.data.data;
      });
    },
    //删除成员
    deleteMember(id) {
      // console.log(id)
      this.$confirm("此操作将移除该成员, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除方法
          deleteMember(id).then((res) => {
            this.$message.success("该成员已被踢出乐队");
            this.membersVisible = false;
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //获取成员列表
    selectMembers(id) {
      // console.log(id);
      //保存乐队id
      this.bandId = id;
      //根据乐队id查询乐队成员信息
      selectMembers(id).then((res) => {
        this.members = res.data.rows;
        // console.log(this.members);
      });
      //打开对话框
      this.membersVisible = true;
    },
    //更新乐队信息
    updateBand() {
      updateBand(this.band)
        .then((res) => {
          this.$message.success("修改成功");
          this.editVisible = false;
          //刷新
          this.getList();
        })
        .catch((err) => {
          this.$message.error(err.data);
        });
    },
    //编辑
    edit(id) {
      //打开窗口
      this.editVisible = true;
      //根据乐队id查询乐队成员信息
      selectMembers(id).then((res) => {
        this.members = res.data.rows;
      });
      //查询乐队信息
      selectBandById(id).then((res) => {
        // console.log("回显成功");
        this.band = res.data.row;
      });
    },
    //添加
    addBand() {
      addBand(this.band)
        .then((res) => {
          this.$message.success("新增乐队成功");
          this.dialogFormVisible = false;
          //刷新
          this.getList();
        })
        .catch((err) => {
          this.$message.error(err.data);
        });
    },
    //添加乐队的弹窗
    addForm() {
      this.band = {};
      this.dialogFormVisible = true;
    },
    //分页查询
    getList(currentPage = 1) {
      //默认第一页
      this.currentPage = currentPage;
      let userId = getToken();
      getBandListPage(userId, this.currentPage, this.pageSize, this.name)
        .then((res) => {
          // console.log(res.data.rows);
          this.total = res.data.total;
          this.list = res.data.rows;
          // console.log(this.total)
          // console.log(this.list)
          this.listLoading = false; //将“数据加载中”这几个字关掉
          console.log("角色：" + this.ROLE);
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleChange(value) {
      console.log(value);
    },
    resetData() {
      //清空查询条件
      this.name = "";
      this.getList();
    },
    removeDataById(id) {
      //删除方法
      //提示信息
      this.$confirm("此操作将永久删除该乐队, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除方法
          deleteBand(id).then((res) => {
            this.$message.success("删除成功");
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.form-container {
  display: flex;
  justify-content: flex-end;
}
</style>